// pages/index/activity/sq-in/index.js
import Dialog from '@vant/weapp/dialog/dialog';
const util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nbTitle: '报名信息',
    nbLoading: false,
    nbFrontColor: '#ffffff',
    nbBackgroundColor: '#3975C6',
    activityDetail: {},
    value: '',
    areaList: [],
    showArea: false,
    showCarType: false,
    nameValue: "",
    phoneValue: "",
    cityValue: "",
    dealerValue: "",
    carTypeValue: "",
    dealerObjcet: [],
    carTypeValueList: [],
    nameErrorMsg: "",
    phoneErrorMsg: "",
    cityErrorMsg: "",
    carTypeErrorMsg: "",
    dealerErrorMsg: "",
    columns: [],
    columnsCity: [],
    dealerColumns: [],
    showAreaField: true,
    showCarField: true,
    showNameField: true,
    showPhoneField: true,
    showDealerField: true,
    showDealer: true,
    showDescriptionField: true,
    description: "",
    carsList: [],
    textareaLimit: {
      maxHeight: 100,
      minHeight: 50
    },
    isSubmiting: false
  },
  
  cancel() {
    wx.navigateBack()
  },
  checkName() {
    let reg = /[\u4e00-\u9fa5]{2,20}/;
    if (this.data.nameValue == "") {
      this.setData({
        nameErrorMsg: "姓名不能为空"
      })
      return false
    } else if (!reg.test(this.data.nameValue) || this.data.phoneValue.length > 20) {
      this.setData({
        nameErrorMsg: "姓名只能是2-25个中文字符"
      })
      return false
    } else {
      this.setData({
        nameErrorMsg: ""
      })
      return true
    }
  },
  checkPhone() {
    let reg = /0?(13|14|15|16|17|18|19)[0-9]{9}/
    if (this.data.phoneValue == "") {
      this.setData({
        phoneErrorMsg: "手机号不能为空"
      })
      return false
    } else if (!reg.test(this.data.phoneValue) || this.data.phoneValue.length > 11) {
      this.setData({
        phoneErrorMsg: "手机号格式错误"
      })
      return false
    } else {
      this.setData({
        phoneErrorMsg: ""
      })
      return true
    }
  },
  isShowField(code) {
    if (this.data.dataList.includes(code)) {
      return true
    }
    return false
  },
  submit() {
    if (this.data.isSubmiting) {
      return
    }

    let form = {
    }
    if (this.data.showNameField) {
      if (this.checkName()) {
        form.name = this.data.nameValue
      } else {
        return
      }
    }
    if (this.data.showPhoneField) {
      if (this.checkPhone()) {
        form.mobile = this.data.phoneValue
      } else {
        return
      }
    }
   console.log(form)
    this.setData({
      isSubmiting: true
    })
    Dialog.alert({
      message: '报名成功',
    }).then(() => {
      wx.navigateBack()
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }

})